use glam::Vec2;
use ggez::{Context, graphics};
use ggez::mint::Point2;

const WIDTH: f32 = 800.0;
const HEIGHT: f32 = 600.0;

pub struct Mover {
    location: Vec2,
    velocity: Vec2,
    acceleration: Vec2
}

impl Mover {
    pub fn new() -> Mover {
        let location = Vec2{x: WIDTH/2.0, y: HEIGHT/2.0};
        let velocity = Vec2{x: 0.0, y: 0.0};
        let acceleration = Vec2 { x: -0.001, y: 0.01 };
        Mover { location, velocity , acceleration}
    }

    pub fn update(&mut self) -> () {
        self.velocity += self.acceleration;
        self.limit(10.0);
        self.location += self.velocity;
    }

    pub fn check_edges(&mut self) -> () {
        if self.location.x > WIDTH { self.location.x = 0.0}
        else if self.location.x < 0.0 {self.location.x = WIDTH}

        if self.location.y > HEIGHT { self.location.y = 0.0}
        else if self.location.y < 0.0 {self.location.y = HEIGHT}
    }

    pub fn display(&self, ctx: &mut Context, canvas : &mut graphics::Canvas) -> () {
        let circle = graphics::Mesh::new_circle(
            ctx,
            graphics::DrawMode::fill(),
            Point2{x: self.location.x, y: self.location.y},
            16.0,
            0.1,
            graphics::Color::new(0.5, 0.5, 0.5, 1.0),
        ).unwrap();
        canvas.draw(&circle, graphics::DrawParam::default());
        let circle = graphics::Mesh::new_circle(
            ctx,
            graphics::DrawMode::stroke(2.0),
            Point2{x: self.location.x, y: self.location.y},
            16.0,
            0.1,
            graphics::Color::BLACK,
        ).unwrap();
        canvas.draw(&circle, graphics::DrawParam::default());
    }

    fn limit(&mut self, top_speed: f32) -> () {
        if self.velocity.length() > top_speed {self.velocity = top_speed*self.velocity.normalize()}
    }
}